package message

import (
	"api/common"
	"api/pb/message"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReadMessageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewReadMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReadMessageLogic {
	return &ReadMessageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// ReadMessage 标记消息已读
func (l *ReadMessageLogic) ReadMessage(req *types.ReadMessageRequest) (resp *types.Empty, err error) {
	// 1. 获取用户标识
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	// 2. 标记消息已读
	_, err = l.svcCtx.MessageRPC.UpdateReadStatus(l.ctx, &message.UpdateReadRequest{
		UserId: userID, ReceiverType: req.ReceiverType, ReceiverId: req.ReceiverID,
	})
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
